package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Article;
import com.example.demo.entity.DTO.ArticleSelectDTO;
import com.example.demo.entity.VO.ForwardVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IArticleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.service.IForwardService;
import com.example.demo.entity.Forward;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 转发文章表 前端控制器
 * </p>
 *
 * @author Mr.Xiayh
 * @since 2024-06-05
 */
@Api(tags = "转发文章接口")
@RestController
@RequestMapping("/forward")
public class ForwardController {

    @Resource
    private IForwardService forwardService;
    @Autowired
    private IArticleService articleService;

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "根据id查询数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @GetMapping("/{id}")
    public Result<Forward> findOne(@PathVariable Integer id) {
        return Result.success(forwardService.getById(id)).msg("查询成功");
    }



    /**
     * 查询文章转发次数
     * @return
     */
    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "查询转发总数接口")
    @GetMapping("/count")
    public Result<Integer> count() {
        return Result.success(forwardService.count()).msg("查询成功");
    }



    /**
     * 根据userId查询该用户转发的文章
     * @param userId
     * @return
     */
    @ApiOperationSupport(author = "Mr.sdy")
    @ApiOperation(value = "根据userId查询该用户转发的文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true)
    })
    @GetMapping("/findArticleByUserId/{userId}")
    public Result<Map<String,Object>> findArticleByUserId(@PathVariable Integer userId) {
        // TODO ID使用token获取

        // 以用户ID查询转发文章ID
        Map<String ,Object> map = new HashMap<>();
        List<Integer> forwardArticleIdByUserId = forwardService.getForwardArticleIdByUserId(userId);
        for (Integer i : forwardArticleIdByUserId) {
            Article article = new Article();
            article.setArticleId(i);
            List<ArticleSelectDTO> allByCriteria = articleService.findAllByCriteria(article, null);
            map.put(String.valueOf(i),allByCriteria);
        }
        return Result.success().msg("查询成功").data(map);
    }



    /**
    * 新增
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "添加转发表")
    @PostMapping("/add")
    public Result<String> add(@RequestBody ForwardVO forwardVO) {
        //TODO 登录功能实现了 参数需要改为userId
//        String tokenValue = StpUtil.getTokenValue();
//        int userId = (int) StpUtil.getLoginIdByToken(tokenValue);
        forwardVO.setUserId(1);
        Forward forward = new Forward();
        BeanUtil.copyProperties(forwardVO,forward);
        if(forward.getUserId() == forwardService.getForwardUserIdByArticleId(forward.getArticleId())){
            return Result.error().msg("不能转发自己的文章");
        }
        if(forwardService.addForward(forward)){
            return Result.success().msg("转发成功");
        }
        return Result.error().msg("转发失败");
    }




}